package com.cqjtu.email;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created by Administrator on 2016/11/26 0026.
 */
public class SendActivity extends Activity{
    private Button btn_sendMail;
    private Button btn_reset;
    private EditText sendAim;
    private EditText subject;
    private EditText mainText;
    private STMPMain stmp;
    boolean check;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Toast.makeText(MyApplication.getContext(), "发送成功!", Toast.LENGTH_SHORT).show();

        }
    };
    Runnable network = new Runnable() {
        @Override
        public void run() {
            try {
                stmp.sendMail();
                Message msg = new Message();
                Bundle data = new Bundle();
                data.putBoolean("value", true);
                msg.setData(data);
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.send_fragment);

        btn_reset = (Button)findViewById(R.id.btn_reset);
        btn_sendMail = (Button) findViewById(R.id.btn_sendMail);
        sendAim = (EditText)findViewById(R.id.id_sendAim);
        subject = (EditText) findViewById(R.id.id_subject);
        mainText = (EditText) findViewById(R.id.id_mainText);

        btn_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendAim.setText("");
                subject.setText("");
                mainText.setText("");
            }
        });

        btn_sendMail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(sendAim.getText().toString()==null||sendAim.getText().toString().equals("")){
                    Toast.makeText(MyApplication.getContext(),"请填写收件人！",Toast.LENGTH_SHORT).show();
                }else if(subject.getText()==null||subject.getText().toString().equals("")){
                    Toast.makeText(MyApplication.getContext(),"请填写主题！",Toast.LENGTH_SHORT).show();
                }else if(mainText.getText()==null||mainText.getText().toString().equals("")){
                    Toast.makeText(MyApplication.getContext(),"请填写内容！",Toast.LENGTH_SHORT).show();
                }else {
                    stmp = new STMPMain(MainActivity.username,sendAim.getText().toString(),MainActivity.password
                            ,subject.getText().toString(),mainText.getText().toString());
                    new Thread(network).start();
                }
            }
        });


    }

}
